package cn.b.sky.common.utils;

import cn.b.sky.common.model.TreeInterface;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TreeUtil {
  
  private TreeUtil() {
    // Hide the constructor
  }

  /**
   * 
   * <strong>Title : gradingRecursion<br>
   * </strong> <strong>Description : </strong>树的父子分离递归方法，适用于非断层的情况，就是父节点必须存在，不然不会解析子节点<br>
   * <strong>Create on : 2015-8-10 下午6:41:25<br>
   * </strong>
   * <p>
   * 
   * @param tempEcardExtends
   * @param parentid
   * @return List<TreeInterfase>
   * @throws
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  public static List<TreeInterface> gradingRecursion(TreeInterface[] tempEcardExtends, String parentid) {

    if (parentid == null) {
      parentid = StringUtils.EMPTY;
    }
    List<TreeInterface> parent = new ArrayList<TreeInterface>();
    List<TreeInterface> children = new ArrayList<TreeInterface>();
    for (TreeInterface tempEcardExtend : tempEcardExtends) {// 父子分离
      if (tempEcardExtend.getPid() == null || tempEcardExtend.getPid().equals(parentid)) {// 父
        parent.add(tempEcardExtend);
      } else {// 子
        children.add(tempEcardExtend);
      }
    }
    if (children.size() == 0) {// 无子，孤独终老
      return parent;
    }
    // 有子，传宗接代，直到断子绝孙
    List<TreeInterface> tempList = null;
    for (TreeInterface tempEcardExtend : parent) {

      tempList = gradingRecursion(children.toArray(new TreeInterface[children.size()]), tempEcardExtend.getNodeId());// 找儿子
      if (tempList != null && tempList.size() != 0) {// 找到儿子
        tempEcardExtend.setTrees(tempList);
      }
    }
    return parent;
  }

  /**
   * 
   * <strong>Title : gradingRecursionSpecial<br></strong>
   * <strong>Description : </strong>树的父子分离递归方法(涉及本身是子元素但父节点不在列表中时  把子元素放到父元素位置，适用于断层情况)<br> 
   * <strong>Create on : 2016-4-6 下午7:24:59<br></strong>
   * <p>
   * @param tempEcardExtends
   * @param parentid
   * @return
   * List<TreeInterface>
   * @throws 
   * @author ZiQiang.Zhao<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  public static List<TreeInterface> gradingRecursionSpecial(TreeInterface[] tempEcardExtends, String parentid) {

    if (parentid == null) {
      parentid = StringUtils.EMPTY;
    }
    List<TreeInterface> parent = new ArrayList<TreeInterface>();
    List<TreeInterface> children = new ArrayList<TreeInterface>();

    List<String> pids = new ArrayList<String>();//父元素ID列表

    for (TreeInterface tempEcardExtend : tempEcardExtends) {// 父子分离
      if (tempEcardExtend.getPid() == null || tempEcardExtend.getPid().equals(parentid)) {// 父
        parent.add(tempEcardExtend);
      } else {// 子
        children.add(tempEcardExtend);
      }
      pids.add(tempEcardExtend.getNodeId());
    }

    //是子元素但父节点不在列表中时  把子元素放到父元素位置
    Iterator<TreeInterface> childrenIter = children.iterator();
    while (childrenIter.hasNext()) {
      TreeInterface tempEcardExtend = childrenIter.next();
      if (!pids.contains(tempEcardExtend.getPid())) {
        parent.add(tempEcardExtend);
        childrenIter.remove();
      }
    }

    if (children.size() == 0) {// 无子，孤独终老
      return parent;
    }
    // 有子，传宗接代，直到断子绝孙
    List<TreeInterface> tempList = null;
    for (TreeInterface tempEcardExtend : parent) {

      tempList = gradingRecursion(children.toArray(new TreeInterface[children.size()]), tempEcardExtend.getNodeId());// 找儿子
      if (tempList != null && tempList.size() != 0) {// 找到儿子
        tempEcardExtend.setTrees(tempList);
      }
    }
    return parent;
  }

  /*public static void main(String[] args) {
    List<Integer> students = new ArrayList<Integer>();
    students.add(1);
    students.add(2);
    students.add(3);
    students.add(4);
    students.add(5);

    //System.out.println(students);
    Iterator<Integer> stuIter = students.iterator();
    while (stuIter.hasNext()) {
      Integer student = stuIter.next();
      if (student == 2) {
        stuIter.remove();
      }
    }
    for (Integer i : students) {

      //System.out.println(i);
    }
  }*/

}
